package com.aw.baselib.base

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Resources
import android.content.res.TypedArray
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.aw.baselib.R
import com.aw.baselib.bean.rx.EventReLoginType
import com.aw.baselib.util.AwActivityUtil.Companion.instance
import com.aw.baselib.util.AwDataUtil.isEmpty
import com.aw.baselib.util.AwLog.d
import com.aw.baselib.util.AwSoftKeyboardUtil.hideIputKeyboard
import com.aw.baselib.util.AwStatusBarUtil.setColor
import com.aw.baselib.util.AwToastUtil.showShort
import com.aw.baselib.widgets.AwViewCustomToolbar
import com.gyf.barlibrary.BarHide
import com.gyf.barlibrary.ImmersionBar
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.Serializable

/**
 * Created by hzw
 */
@SuppressLint("NewApi")
abstract class AwBaseActivity : AppCompatActivity(), AwBaseView, View.OnClickListener {
    protected var mActivity: Activity? = this@AwBaseActivity
    var TAG = AwBaseActivity::class.java.simpleName
    protected var mLoadingDialog: AwViewLoadingDialog? = null
    protected var mDialog: AwViewDialog? = null
    protected var mSavedInstanceState: Bundle? = null
    protected var mToolbar: AwViewCustomToolbar? = null
    protected abstract val layoutId: Int
    protected fun initView() {}
    protected fun initData() {}
    protected fun initListener() {}
    protected fun reLogin() {}
    protected fun beforeAddView() {}
    var myFragmentManager: FragmentManager? = null
        private set

    override fun onCreate(savedInstanceState: Bundle?) {
        mSavedInstanceState = savedInstanceState
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating) {
            val result = fixOrientation()
        }
        beforeAddView()
        super.onCreate(savedInstanceState)
        TAG = javaClass.simpleName
        if (AwBaseConstant.LAYOUT_TRANSPARENT != layoutId) {
            setContentView(layoutId)
        }
        mActivity = this
        //该方法是【友盟+】Push后台进行日活统计及多维度推送的必调用方法，请务必调用！
//        if(PushAgent.getInstance(mActivity) != null) {
//            PushAgent.getInstance(mActivity).onAppStart();
//            MobclickAgent.setCatchUncaughtExceptions(false);//关闭错误统计
//        }
        instance!!.addActivity(mActivity as AwBaseActivity)
        myFragmentManager = supportFragmentManager
        setStatusTransparentDarkTxt()
        initView()
        initData()
        initListener()
        EventBus.getDefault().register(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refreshByBus(type: AwBaseEvent?) {
        if (type is EventReLoginType) {
//            if(AwActivityUtil.isTopActivity(mActivity) && AwBaseApplication.getInstance().isAllowShowReloginDialog()) {
//                showDialog("登录过期，请重新登录", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dismissDialog();
//                        AwBaseApplication.getInstance().needReLogin();
////                    reLogin();
//                    }
//                });
//            }
        }
    }

    protected fun postEvent(event: AwBaseEvent?) {
        EventBus.getDefault().postSticky(event)
    }

    override fun showLoadingDialog() {
        if (mActivity == null) {
            return
        }
        runOnUiThread {
            if (mLoadingDialog == null) {
                mLoadingDialog = AwViewLoadingDialog(mActivity)
            }
            if (!mLoadingDialog!!.isShowing) {
                mLoadingDialog!!.show()
            }
        }
    }

    override fun dismissLoadingDialog() {
        if (mActivity == null) {
            return
        }
        runOnUiThread {
            if (mLoadingDialog != null && mLoadingDialog!!.isShowing) {
                mLoadingDialog!!.dismiss()
            }
        }
    }

    fun dismissDialog() {
        if (mDialog != null) {
            mDialog!!.dismiss()
        }
    }

    fun initDialog() {
        if (mDialog != null) {
            mDialog!!.dismiss()
        }
        if (mDialog == null) {
            mDialog = mActivity?.let { AwViewDialog(it) }
        }
    }

    fun showDialogToFinish(msg: String) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        mDialog!!.showCommonDialog(
            msg,
            getString(R.string.common_cancel),
            getString(R.string.common_close),
            false,
            false,
            null
        ) { finish() }
    }

    fun showDialogToBack(msg: String) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        mDialog!!.showCommonDialog(
            msg,
            getString(R.string.common_cancel),
            getString(R.string.common_close),
            false,
            false,
            null
        ) { fragmentToBack() }
    }

    fun showLoadingDialogWithMsg(msg: String) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        runOnUiThread {
            if (mLoadingDialog == null) {
                mLoadingDialog = AwViewLoadingDialog(mActivity)
            }
            if (!mLoadingDialog!!.isShowing) {
                mLoadingDialog!!.show()
            }
            mLoadingDialog!!.showMsg(msg)
        }
    }

    fun showDialog(msg: String) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                getString(R.string.common_cancel),
                getString(R.string.common_close),
                false,
                false,
                null,
                null
            )
        }
    }

    fun showDialog(msg: String, listener: View.OnClickListener?) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                getString(R.string.common_cancel),
                getString(R.string.common_close),
                false,
                false,
                null,
                listener
            )
        }
    }

    fun showDialogWithCancelDismiss(msg: String, confirmListener: View.OnClickListener?) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                getString(R.string.common_cancel),
                getString(R.string.common_close),
                false,
                true,
                null,
                confirmListener
            )
        }
    }

    fun showDialogWithCancelFinish(msg: String, confirmListener: View.OnClickListener?) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                getString(R.string.common_cancel),
                getString(R.string.common_confirm),
                false,
                true,
                View.OnClickListener { finish() },
                confirmListener
            )
        }
    }

    fun showDialogCustomLeftAndRight(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                leftTxt,
                rightTxt,
                false,
                true,
                leftListener,
                rightListener
            )
        }
    }

    fun showInputDialog(
        title: String?,
        hintMsg: String?,
        showCancelBtn: Boolean,
        callback: AwViewDialog.InputCallback?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread { mDialog!!.showInputDialog(title, hintMsg, showCancelBtn, callback) }
    }

    fun showInputDialogWithOld(
        title: String?,
        text: String,
        callback: AwViewDialog.InputCallback?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread { mDialog!!.showInputDialogWithOld(title, text!!, callback) }
    }

    fun showCommonCustomDialog(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                leftTxt,
                rightTxt,
                showTopImg,
                showCancelBtn,
                leftListener,
                rightListener
            )
        }
    }

    fun showCommonCustomDialog(
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        allowCancelOnTouchOutside: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                leftTxt,
                rightTxt,
                showTopImg,
                showCancelBtn,
                allowCancelOnTouchOutside,
                leftListener,
                rightListener
            )
        }
    }

    fun showCommonCustomDialog(
        title: String?,
        msg: String?,
        leftTxt: String?,
        rightTxt: String?,
        showTopImg: Boolean,
        showCancelBtn: Boolean,
        allowCancelOnTouchOutside: Boolean,
        leftListener: View.OnClickListener?,
        rightListener: View.OnClickListener?
    ) {
        if (mActivity == null) {
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                title,
                msg,
                leftTxt,
                rightTxt,
                showTopImg,
                showCancelBtn,
                allowCancelOnTouchOutside,
                leftListener,
                rightListener
            )
        }
    }

    fun showErrorDialog(msg: String, closeListener: View.OnClickListener?) {
        if (mActivity == null) {
            showMsg(msg)
            return
        }
        initDialog()
        runOnUiThread {
            mDialog!!.showCommonDialog(
                msg,
                getString(R.string.common_cancel),
                getString(R.string.common_close),
                true,
                false,
                null,
                closeListener
            )
        }
    }

    //========================APP定制部分结束================================
    fun showMsg(msg: String?, defaultMsg: String?) {
        runOnUiThread {
            showShort(
                AwBaseApplication.instance,
                if (isEmpty(msg)) defaultMsg else msg
            )
        }
    }

    override fun showMsg(msg: String?) {
        runOnUiThread { showShort(AwBaseApplication.instance, msg) }
    }

    fun showMsg(msgId: Int) {
        runOnUiThread { showShort(AwBaseApplication.instance, getString(msgId)) }
    }

    fun setText(tv: TextView?, content: String?) {
        if (null == tv) {
            return
        }
        if (isEmpty(content)) {
            tv.text = "--"
        } else {
            tv.text = content
        }
    }

    protected fun setTextNoDefault(tv: TextView?, content: String?) {
        if (null == tv) {
            return
        }
        if (isEmpty(content)) {
            tv.text = ""
        } else {
            tv.text = content
        }
    }

    protected fun getText(tv: TextView?): String {
        if (null == tv) {
            return ""
        }
        return if (isEmpty(tv.text.toString())) {
            ""
        } else tv.text.toString().trim { it <= ' ' }
    }

    override fun onClick(v: View) {}
    fun toFinish() {
        //        finish();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            finishAfterTransition()
        } else {
            finish()
        }
    }

    /**
     * 重写 getResource 方法，防止系统字体影响
     */
    override fun getResources(): Resources { //禁止app字体大小跟随系统字体大小调节
        val resources = super.getResources()
        if (resources != null && resources.configuration.fontScale != 1.0f) {
            val configuration = resources.configuration
            configuration.fontScale = 1.0f
            resources.updateConfiguration(configuration, resources.displayMetrics)
        }
        return resources
    }

    /**
     * 跳转activity
     *
     * @param clazz
     * @param params 传值格式  key,value
     */
    fun toClassNoAnim(clazz: Class<*>?, needFinish: Boolean, vararg params: Any) {
        if (params != null) {
            startActivity(getActivityIntent(clazz, params))
            //            overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
            overridePendingTransition(0, 0)
            if (needFinish) {
                toFinish()
            }
        }
    }

    /**
     * 跳转activity
     *
     * @param clazz
     * @param params 传值格式  key,value
     */
    fun toClass(clazz: Class<*>?, needFinish: Boolean, vararg params: Any?) {
        if (params != null) {
            startActivity(getActivityIntent(clazz, params))
            //            overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
            //            overridePendingTransition(R.anim.slide_in_from_right, 0);
            //            overridePendingTransition(0, 0);
            if (needFinish) {
                toFinish()
            }
        }
    }

    /**
     * 跳转activity(不传参)
     *
     * @param clazz
     */
    fun toClass(clazz: Class<*>?, needFinish: Boolean) {
        startActivity(getActivityIntent(clazz, null))
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        overridePendingTransition(0, 0)
        if (needFinish) {
            toFinish()
        }
    }

    fun toClassForResult(clazz: Class<*>?, requestCode: Int) {
        startActivityForResult(getActivityIntent(clazz, null), requestCode)
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        //        overridePendingTransition(0, 0);
    }

    fun toClassForResult(clazz: Class<*>?, requestCode: Int, vararg params: Any?) {
        startActivityForResult(getActivityIntent(clazz, params), requestCode)
        //        overridePendingTransition(R.anim.anim_none2, R.anim.anim_none2);
        //        overridePendingTransition(R.anim.slide_in_from_right, 0);
        //        overridePendingTransition(0, 0);
    }

    fun getActivityIntent(var1: Class<*>?, vararg var2: Any?): Intent {
        val var3 = Intent(mActivity, var1)
        return if (var2 == null || var2.size == 0) {
            var3
        } else {
            var var4 = var2.size
            if (var4 % 2 != 0) {
                showMsg("参数格式为key,value")
            } else {
                var4 /= 2
                for (var5 in 0 until var4) {
                    val var6 = var2[var5 + var5]
                    val var7 = var2[var5 + var5 + 1]
                    if (var6 !is String) {
                        showMsg("参数key类型不对")
                        return var3
                    }
                    val var8 = var6
                    if (var7 is String) {
                        var3.putExtra(var8, var7 as String?)
                    } else if (var7 is Int) {
                        var3.putExtra(var8, var7 as Int?)
                    } else if (var7 is Boolean) {
                        var3.putExtra(var8, var7 as Boolean?)
                    } else if (var7 is Parcelable) {
                        var3.putExtra(var8, var7 as Parcelable?)
                    } else if (var7 is Serializable) {
                        var3.putExtra(var8, var7 as Serializable?)
                    }
                }
            }
            var3
        }
    }

    /**
     * 跳转fragment
     *
     * @param params 传值格式  key,value
     */
    fun toClass(viewId: Int, fragment: Fragment, addBackStack: Boolean, vararg params: Any?) {
        val transaction = myFragmentManager!!.beginTransaction()
        val isTagNull = null == myFragmentManager!!.findFragmentByTag(fragment.javaClass.name)
        d("toClass, fragment: " + fragment.javaClass.name + " ,isAdded: " + fragment.isAdded + " ,findTag is null?: " + isTagNull)
        if (!fragment.isAdded && null == myFragmentManager!!.findFragmentByTag(fragment.javaClass.name)) {
            myFragmentManager!!.executePendingTransactions()
            if (params != null) {
                try {
                    fragment.arguments = getFragmentBundle(params)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
            if (addBackStack) {
                transaction.add(viewId, fragment, fragment.javaClass.name)
                transaction.addToBackStack(fragment.javaClass.name)
            } else {
                transaction.replace(viewId, fragment, fragment.javaClass.name)
            }
            transaction.commitAllowingStateLoss()
        } else {
            d("toClassChild, fragment: " + fragment.javaClass.name + " 已经存在, 直接展示出来")
            transaction.show(fragment).commitAllowingStateLoss()
        }
    }

    /**
     * 跳转fragment(不传参)
     */
    fun toClass(viewId: Int, fragment: Fragment, addBackStack: Boolean) {
        toClass(viewId, fragment, addBackStack, *arrayOfNulls<Any>(0))
    }

    fun getFragmentBundle(vararg var2: Any?): Bundle {
        val bundle = Bundle()
        return if (var2 == null || var2.size == 0) {
            bundle
        } else {
            var var4 = var2.size
            if (var4 % 2 != 0) {
                showMsg("参数格式为key,value")
            } else {
                var4 /= 2
                for (var5 in 0 until var4) {
                    val var6 = var2[var5 + var5]
                    val var7 = var2[var5 + var5 + 1]
                    if (var6 !is String) {
                        showMsg("参数key类型不对")
                        return bundle
                    }
                    val var8 = var6
                    if (var7 is String) {
                        bundle.putString(var8, var7 as String?)
                    } else if (var7 is Int) {
                        bundle.putInt(var8, (var7 as Int?)!!)
                    } else if (var7 is Boolean) {
                        bundle.putBoolean(var8, (var7 as Boolean?)!!)
                    } else if (var7 is Parcelable) {
                        bundle.putParcelable(var8, var7 as Parcelable?)
                    } else if (var7 is Serializable) {
                        bundle.putSerializable(var8, var7 as Serializable?)
                    }
                }
            }
            bundle
        }
    }

    fun isShow(v: View?): Boolean {
        return if (v == null) false else v.visibility == View.VISIBLE
    }

    fun showView(v: View?, isShow: Boolean) {
        if (v != null) {
            if (isShow) {
                v.visibility = View.VISIBLE
            } else {
                v.visibility = View.GONE
            }
        }
    }

    fun showViewInvisible(v: View?, isShow: Boolean) {
        if (v != null) {
            if (isShow) {
                v.visibility = View.VISIBLE
            } else {
                v.visibility = View.INVISIBLE
            }
        }
    }

    val visibleFragment: Fragment?
        get() {
            @SuppressLint("RestrictedApi") val fragments = myFragmentManager!!.fragments
            if (fragments != null && fragments.size > 0) {
                for (fragment in fragments) {
                    if (fragment != null && fragment.isVisible) {
                        return fragment
                    }
                }
            }
            return null
        }

    override fun onBackPressed() {
        super.onBackPressed()
        if (mLoadingDialog != null && mLoadingDialog!!.isShowing) {
            mLoadingDialog!!.dismiss()
        }
        toBack()
    }

    fun toBack() {
        if (hasPopBackStack()) {
            myFragmentManager!!.popBackStack()
        } else {
            finish()
        }
    }

    fun hasPopBackStack(): Boolean {
        return myFragmentManager!!.backStackEntryCount > 0
    }

    fun fragmentToBack() {
        myFragmentManager!!.popBackStack()
    }

    fun popAllToBack() {
        val count = myFragmentManager!!.backStackEntryCount
        d("popAllToBack count: $count")
        if (count > 0) {
            for (i in 0 until count) {
                val backstatck = myFragmentManager!!.getBackStackEntryAt(i)
                d("popAllToBack Fragment", backstatck.name)
                myFragmentManager!!.popBackStack()
            }
            //            getSupportFragmentManager().popBackStackImmediate(fragment.getClass().getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    /**
     * 强制退出程序
     */
    fun ExitAppForced() {
        dismissLoadingDialog()
        //        ActCollector.release();
        //        mApp.clearService();
        System.exit(0)
    }

    fun replaceFragment(id: Int, fragment: Fragment?, isAddBack: Boolean) {
        val ft = myFragmentManager!!.beginTransaction()
        ft.replace(id, fragment!!)
        if (isAddBack) {
            ft.addToBackStack(null)
        }
        ft.commitAllowingStateLoss()
    }

    fun replaceFragmentArgs(
        id: Int, fragment: Fragment, obj: Any?,
        isAddBack: Boolean
    ) {
        val b = Bundle()
        b.putSerializable("params", obj as Serializable?)
        fragment.arguments = b
        replaceFragment(id, fragment, isAddBack)
    }

    protected fun setToolbar(
        title: String?,
        leftClickListener: AwViewCustomToolbar.OnLeftClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            mToolbar!!.hideRightView()
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            if (leftClickListener == null) {
                mToolbar!!.setOnLeftClickListener(object : AwViewCustomToolbar.OnLeftClickListener {
                    override fun onLeftTextClick() {
                        try {
                            hideIputKeyboard(mActivity!!)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                        finish()
                    }
                })
            } else {
                mToolbar!!.setOnLeftClickListener(leftClickListener)
            }
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
        }
    }

    protected fun setToolbarWithBackImg(
        title: String?,
        leftClickListener: AwViewCustomToolbar.OnLeftClickListener?
    ) {
        setToolbar(title, leftClickListener)
        if (mToolbar != null) {
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            mToolbar!!.hideRightView()
        }
    }

    protected fun setToolbarHideLeftAndRight(
        title: String?,
        leftClickListener: AwViewCustomToolbar.OnLeftClickListener?
    ) {
        setToolbar(title, leftClickListener)
        if (mToolbar != null) {
            mToolbar!!.hideLeftView()
            mToolbar!!.hideRightView()
        }
    }

    protected fun setToolbarWithBackWithRightImg(
        title: String?,
        viewId: Int,
        rightClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        setToolbar(title, null)
        if (mToolbar != null) {
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            mToolbar!!.showRightView()
            mToolbar!!.setRightImg(viewId)
            mToolbar!!.setOnRightClickListener(rightClickListener)
        }
    }

    protected fun setToolbarHideLeftWithRightImg(
        title: String?,
        viewId: Int,
        rightClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        setToolbar(title, null)
        if (mToolbar != null) {
            mToolbar!!.hideLeftView()
            mToolbar!!.showRightView()
            mToolbar!!.setRightImg(viewId)
            mToolbar!!.setOnRightClickListener(rightClickListener)
        }
    }

    protected fun setToolbarHideLeftWithRightTxt(
        title: String?,
        txt: String?,
        rightClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        setToolbar(title, null)
        if (mToolbar != null) {
            mToolbar!!.hideLeftView()
            mToolbar!!.showRightView()
            mToolbar!!.setRightText(txt)
            mToolbar!!.setOnRightClickListener(rightClickListener)
        }
    }

    protected fun setToolbarHideLeftWithRightTxtImg(
        title: String?,
        txt: String?,
        viewId: Int,
        rightClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        setToolbar(title, null)
        if (mToolbar != null) {
            mToolbar!!.hideLeftView()
            mToolbar!!.showRightView()
            mToolbar!!.setRightImgAndText(viewId, txt)
            mToolbar!!.setOnRightClickListener(rightClickListener)
        }
    }

    protected fun setToolbarWithBackCustomImgAndRightTxtImg(
        title: String?, leftViewId: Int, rightTxt: String?, rightViewId: Int,
        leftClickListener: AwViewCustomToolbar.OnLeftClickListener?,
        rigthClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
            mToolbar!!.setLeftImg(leftViewId)
            mToolbar!!.setRightImgAndText(rightViewId, rightTxt)
            if (null != leftClickListener) {
                mToolbar!!.setOnLeftClickListener(leftClickListener)
            }
            if (null != rigthClickListener) {
                mToolbar!!.setOnRightClickListener(rigthClickListener)
            }
        }
    }

    protected fun setToolbarWithBackCustomImgAndRightTxt(
        title: String?, leftViewId: Int, rightTxt: String?,
        leftClickListener: AwViewCustomToolbar.OnLeftClickListener?,
        rigthClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
            mToolbar!!.setLeftImg(leftViewId)
            mToolbar!!.setRightText(rightTxt)
            if (null != leftClickListener) {
                mToolbar!!.setOnLeftClickListener(leftClickListener)
            }
            if (null != rigthClickListener) {
                mToolbar!!.setOnRightClickListener(rigthClickListener)
            }
        }
    }

    protected fun setToolbarWithBackImgAndRightView(
        title: String?,
        rightTxt: String?,
        rigthClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            mToolbar!!.setRightText(rightTxt)
            mToolbar!!.setOnLeftClickListener(object : AwViewCustomToolbar.OnLeftClickListener {
                override fun onLeftTextClick() {
                    try {
                        hideIputKeyboard(mActivity!!)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    finish()
                }
            })
            if (null != rigthClickListener) {
                mToolbar!!.setOnRightClickListener(rigthClickListener)
            }
        }
    }

    protected fun setToolbarWithBackImgAndRightTextHasColor(
        title: String?,
        rightTxt: String?,
        colorRes: Int,
        rigthClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            mToolbar!!.setRightTextWithColor(rightTxt, colorRes)
            mToolbar!!.setOnLeftClickListener(object : AwViewCustomToolbar.OnLeftClickListener {
                override fun onLeftTextClick() {
                    try {
                        hideIputKeyboard(mActivity!!)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    finish()
                }
            })
            if (null != rigthClickListener) {
                mToolbar!!.setOnRightClickListener(rigthClickListener)
            }
        }
    }

    protected fun setToolbarWithBackImgAndRightView(
        title: String?,
        rightImg: Int,
        rigthClickListener: AwViewCustomToolbar.OnRightClickListener?
    ) {
        mToolbar = findViewById<View>(R.id.toolbar_custom) as AwViewCustomToolbar
        if (mToolbar != null) {
            if (!isEmpty(title)) {
                mToolbar!!.setToolbarTitle(title)
            } else {
                mToolbar!!.hideTitleView()
            }
            mToolbar!!.setLeftImg(R.drawable.icon_back)
            mToolbar!!.setRightImg(rightImg)
            mToolbar!!.setOnLeftClickListener(object : AwViewCustomToolbar.OnLeftClickListener {
                override fun onLeftTextClick() {
                    try {
                        hideIputKeyboard(mActivity!!)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    finish()
                }
            })
            if (null != rigthClickListener) {
                mToolbar!!.setOnRightClickListener(rigthClickListener)
            }
        }
    }

    protected fun setStatusColor(colorRes: Int) {
        if (mToolbar != null) {
            mToolbar!!.setBackgroundColor(colorRes)
            setColor(mActivity!!, colorRes, 0) //0 透明 255 黑色
        }
    }

    /**
     * 筛选部分页面, 不允许更改状态栏颜色值
     * @return
     */
    private fun filterActivity(): Boolean {
//        if(mActivity instanceof MainActivity
//                || mActivity instanceof NestedScrollingActivity) {
//            return false;
//        }
        return true
    }

    fun showToDevMsg() {
        showMsg(R.string.common_process_dev)
    }

    protected fun setStatusTransparent() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(false)
            .transparentStatusBar()
            .statusBarDarkFont(false)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusTransparentDarkTxt() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(false)
            .transparentStatusBar()
            .statusBarDarkFont(true)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusTransparentWithNavigationBar() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(false)
            .transparentNavigationBar()
            .statusBarDarkFont(false)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusHideNavigationBar() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(false)
            .hideBar(BarHide.FLAG_HIDE_BAR)
            .statusBarDarkFont(false)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusTxtDark() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(true)
            .statusBarDarkFont(true, 0.2f)
            .statusBarColor(R.color.white)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusToSpecialColor(color: Int) {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(true)
            .statusBarDarkFont(true, 0.2f)
            .statusBarColor(color)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusBlue() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(true)
            .statusBarDarkFont(false)
            .statusBarColor(R.color.colorPrimary)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusWhite() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(true)
            .statusBarDarkFont(false)
            .statusBarColor(R.color.white)
            .keyboardEnable(true)
            .init()
    }

    protected fun setStatusBlack() {
        ImmersionBar.with(mActivity!!).fitsSystemWindows(true)
            .statusBarDarkFont(false)
            .statusBarColor(R.color.black, 0.5f)
            .keyboardEnable(true)
            .init()
    }

    /** 针对8.0设备, 透明Activity会导致Only fullscreen opaque activities can request orientation, 参考https://blog.csdn.net/starry_eve/article/details/82777160解决如下  */
    @SuppressLint("WrongConstant")
    private fun fixOrientation(): Boolean {
        try {
            val field = Activity::class.java.getDeclaredField("mActivityInfo")
            field.isAccessible = true
            val o = field[this] as ActivityInfo
            o.screenOrientation = -1
            field.isAccessible = false
            return true
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    private val isTranslucentOrFloating: Boolean
        private get() {
            var isTranslucentOrFloating = false
            try {
                val styleableRes = Class.forName("com.android.internal.R\$styleable")
                    .getField("Window")[null] as IntArray
                val ta = obtainStyledAttributes(styleableRes)
                val m = ActivityInfo::class.java.getMethod(
                    "isTranslucentOrFloating",
                    TypedArray::class.java
                )
                m.isAccessible = true
                isTranslucentOrFloating = m.invoke(null, ta) as Boolean
                m.isAccessible = false
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return isTranslucentOrFloating
        }

    /** 针对8.0设备, 透明Activity会导致Only fullscreen opaque activities can request orientation, 参考https://blog.csdn.net/starry_eve/article/details/82777160解决如上  */
    override fun setRequestedOrientation(requestedOrientation: Int) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating) {
            return
        }
        super.setRequestedOrientation(requestedOrientation)
    }

    override fun onDestroy() {
        super.onDestroy()
        instance!!.removeActivity(mActivity!!)
        EventBus.getDefault().unregister(this)
        ImmersionBar.with(mActivity!!).destroy()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }
}